package com.cxhome.cxinfox.agentProsDiscount;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.traffic.managent.Constants;
import com.traffic.managent.base.BaseService;
import com.traffic.managent.base.Paginator;
import com.traffic.managent.model.account.Account;
import com.traffic.managent.model.system.Department;
import com.traffic.managent.model.system.UserProsDiscountInfo;
import com.traffic.managent.util.SQLUtils;
import com.traffic.managent.util.UtilsUtils;

/**
 * 产品折扣处理类。
 * 
 */
public class UserProsDiscountInfoService extends BaseService {

	private static Logger log = Logger.getLogger(UserProsDiscountInfoService.class);
	public static final UserProsDiscountInfoService service = new UserProsDiscountInfoService();

	public Page<UserProsDiscountInfo> paginate(UserProsDiscountInfo model, Paginator paginator, String departmentId,
			String orderSql) {

		String tempIdsStr = Department.dao.queryChildIds(departmentId);
		String tempIdsStr1 = tempIdsStr.replaceAll(",", "','");
		String idsStr = "";
		if (tempIdsStr1.contains(",")) {
			idsStr = "('" + tempIdsStr1 + "')";
		} else {// 组织机构只有一个时查询语句会报错
			idsStr = "('" + tempIdsStr1 + "','" + tempIdsStr1 + "')";
		}

		SQLUtils sql = new SQLUtils(
				" FROM cx_user_pros_discount_info a, cx_customer_info b, cx_product_vas c , cx_user u  "
						+ "WHERE a.customerId = b.customerId AND a.del_flag = '0' AND a.proId = c.ids AND c.status = '0'  and u.customerId = b.customerId ");

		if (model.getAttrValues().length != 0) {
			sql.whereLike("b.customerId", model.getStr("customerId"));
			sql.whereLike("c.vasProdName", model.getStr("proId"));
		}
		sql.append(orderSql);
		
		return UserProsDiscountInfo.dao.paginate(paginator, "select DISTINCT a.*,b.customerName, c.vasProdName  ", sql.toString());

	}

	/**
	 * 条件查询折扣
	 * 
	 * @param customerId
	 * @param productIds
	 * @return
	 */
	public List<UserProsDiscountInfo> findByParam(String customerId, String productIds) {

		String sql = "SELECT * from cx_user_pros_discount_info u where 1=1 AND u.customerId = ? AND u.proId = ? ORDER BY create_time desc, begin_time DESC";

		return UserProsDiscountInfo.dao.find(sql, customerId, productIds);
	}

	/**
	 * 删除：更新状态
	 */
	public void delete(String name, String ids) {
		try {
			Db.update(
					"update cx_user_pros_discount_info set del_flag = '1', oper_time = now(),oper_user = ? where 1=1 and ids = ? ",
					name, ids);
		} catch (Exception e) {
			log.error("折扣产品id为" + ids + "删除异常" + e);
		}
	}

	/**
	 * 新增/编辑
	 */
	@Before(Tx.class)
	public String saveOrUpdate(UserProsDiscountInfo model, String ids, String oper_id) {
		String result = Constants.OPER_SUCCESS;
		UserProsDiscountInfo modelDB = UserProsDiscountInfo.dao.findById(model.getStr("ids"));
		try {
			// 更新。先更新当前数据
			if (!"".equals(ids) && ids != null) {

				modelDB.set("del_flag", "1");
				Date date = new Date();
				modelDB.set("create_time", modelDB.getDate("create_time"));
				modelDB.set("end_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
				modelDB.set("ids", "edit" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
				modelDB.save();

				model.set("begin_time", date);
				model.set("oper_user", oper_id);
				model.set("oper_time", new Date());
				model.set("create_time", date);
				model.update();
			} else { // 新增
				String proidsDiscounts = model.getStr("proId");
				String[] proidsDiscountstr = proidsDiscounts.split("\\+");
				for (int i = 0; i < proidsDiscountstr.length; i++) {
					// 每一个产品+折扣
					String proidsDiscount = proidsDiscountstr[i];
					String proId = proidsDiscount.split("\\-")[0];
					String discount = proidsDiscount.split("\\-")[1];
					model.put("discount", discount);
					model.put("proId", proId);
					model.put("oper_user", oper_id);
					model.put("create_time", new Date());
					model.put("ids", UtilsUtils.getRandomId(30));
					model.save();
				}
			}

		} catch (Exception e) {
			result = Constants.OPER_FAIL;
			log.error("操作异常：" + e);
		}
		return result;

	}

}
